package com.zn.utils
{
    import com.zn.ResLoader;
    import com.zn.bitmapDisplay.BitmapDataFunc;
    import com.zn.bitmapDisplay.BitmapMovieClip;
    import com.zn.loading.ImageLoader;
    import com.zn.loading.LoaderEvent;
    import com.zn.loading.LoaderMax;
    
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.utils.getDefinitionByName;

    public class ClassUtil
    {

        private static var _displayObjectLoadDic:Object = {};

        public static function getObject(className:String):*
        {
            try
            {
                var classObj:Class = Class(getDefinitionByName(className));
				var obj:*=new classObj();
				
				if(obj is BitmapData)
	                return new Bitmap(obj);
				
				return obj;
            }
            catch (error:Error)
            {

            }
            return null;
        }

        /**
         *加载并创建对象
         * @param url
         * @param className
         * @param callBack
         * @param isBitmapMC
         * @param isFrameMC
         *
         */
        public static function getDisplayObjectByLoad(url:String, className:String, callBack:Function, isBitmapMC:Boolean = false, isFrameMC:Boolean = false):void
        {
			if(StringUtil.isEmpty(url))
				return ;
			
            var urlList:Array = _displayObjectLoadDic[url];
            if (urlList == null)
                urlList = _displayObjectLoadDic[url] = [];

            var loaderVO:ClassUtilLoaderVO = new ClassUtilLoaderVO();
            loaderVO.url = url;
            loaderVO.className = className;
            loaderVO.callBack = callBack;
            loaderVO.isBitmapMC = isBitmapMC;
            loaderVO.isFrameMC = isFrameMC;
            urlList.push(loaderVO);

            ResLoader.load(url, "", getDisplayObjectByLoadComplete, false, false, null, getDisplayObjectByLoadIoError);
        }

        private static function getDisplayObjectByLoadComplete(event:LoaderEvent):void
        {
            try
            {
                var url:String = event.currentLoader.url;
                var urlList:Array = _displayObjectLoadDic[url];
                var loaderVO:ClassUtilLoaderVO;

                if (urlList)
                {
                    for (var i:int = 0; i < urlList.length; i++)
                    {
                        loaderVO = urlList[i];

                        var classObj:Class = Class(getDefinitionByName(loaderVO.className));
                        var mc:MovieClip = new classObj();
                        var displayObjec:*;
                        if (loaderVO.isBitmapMC)
                        {
                            if (loaderVO.isFrameMC)
                                displayObjec = BitmapDataFunc.createBitmapMCByMCFrame(mc);
                            else
                                displayObjec = new BitmapMovieClip(BitmapDataFunc.createBitmapDataByMC(mc));
                        }
                        else
                            displayObjec = mc;

                        loaderVO.displayObject = displayObjec;
                        loaderVO.callBack(loaderVO);
                    }

                    _displayObjectLoadDic[url] = null;
                }
            }
            catch (error:Error)
            {

            }
        }

        private static function getDisplayObjectByLoadIoError(event:LoaderEvent):void
        {
            delete _displayObjectLoadDic[event.currentLoader.url];
        }

        /**
         *加载图片
         * @param url
         * @param callBack
         *
         */
        public static function getImageByLoad(url:String, callBack:Function):void
        {
			if(StringUtil.isEmpty(url))
				return ;
			
            var loaderVO:ClassUtilLoaderVO = new ClassUtilLoaderVO();
            loaderVO.url = url;
            loaderVO.callBack = callBack;

            var urlList:Array = _displayObjectLoadDic[url];
            if (urlList == null)
                urlList = _displayObjectLoadDic[url] = [];

            urlList.push(loaderVO);

            ResLoader.load(url, "", getImageByLoadComplete, false, false, null, getDisplayObjectByLoadIoError);
        }

        private static function getImageByLoadComplete(event:LoaderEvent):void
        {
            try
            {
                var url:String = event.currentLoader.url;
                var urlList:Array = _displayObjectLoadDic[url];
                var loaderVO:ClassUtilLoaderVO;
                var imageLoad:ImageLoader = event.currentLoader as ImageLoader;

                if (urlList)
                {
                    for (var i:int = 0; i < urlList.length; i++)
                    {
                        loaderVO = urlList[i];

                        loaderVO.displayObject = imageLoad.displayObject;

                        loaderVO.callBack(loaderVO);
                    }

                    _displayObjectLoadDic[url] = null;
                }
            }
            catch (error:Error)
            {

            }
        }
    }
}
